package com.zyl.Ecs.realtime.service.impl;

import com.zyl.Ecs.common.domain.device.vo.SensorVO;
import com.zyl.Ecs.common.response.Result;
import com.zyl.Ecs.common.utils.CUtil;
import com.zyl.Ecs.realtime.client.DeviceClient;
import com.zyl.Ecs.realtime.datasource.DataSourceContextHolder;
import com.zyl.Ecs.realtime.datasource.Master;
import com.zyl.Ecs.realtime.mapper.TableMapper;
import com.zyl.Ecs.realtime.service.TableService;
import com.zyl.Ecs.realtime.util.TableUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.realtime.service.impl
 * @ClassName: TableServiceImpl
 * @Description TODO
 * @Author 翟玉龙
 * @Date 2024/12/6-13:34
 */
@Service
@RequiredArgsConstructor
public class TableServiceImpl implements TableService {

    private final TableMapper tableMapper;

    private final DeviceClient deviceClient;

    /**
     * 创建当前月每个设备的数据表格
     */
    @Override
    public void createCurrentMonthTable() {
        // 当前年份和月份
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        // 执行创建
        createSpecificTable(year, month);
    }

    /**
     * 创建下个月每个设备的数据表格
     */
    @Override
    public void createNextMonthTable() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 获取当前年份
        int year = now.getYear();
        // 获取下一个月份
        int month = now.getMonthValue() + 1;
        // 如果当前月已经是12月，则年份+1，月份置为1
        if (month > 12) {
            year += 1;
            month = 1;
        }
        createSpecificTable(year, month);
    }

    /**
     * 创建指定日期每个设备的数据表格
     *
     * @param year  年份
     * @param month 月份
     */
    @Override
    public void createSpecificTable(int year, int month) {
        /* 调用设备服务，查询所有传感器列表 */
        Result<?> result = deviceClient.listSensors();
        Object data = result.getData();
        /* 绝对安全转型 */
        if (data instanceof List) {
            List<?> list = (List<?>) data;
            for (Object element : list) {
                if (element instanceof SensorVO) {
                    SensorVO sensorVO = (SensorVO) element;
                    createSpecificTable(year, month, sensorVO.getId());
                }
            }
        }
    }

    /**
     * 创建指定日期、指定设备的数据表格
     *
     * @param year     年份
     * @param month    月份
     * @param sensorId 传感器ID
     */
    @Master
    @Override
    public void createSpecificTable(int year, int month, int sensorId) {
        DataSourceContextHolder.master();
        String tableName = TableUtil.tableName(year, month, sensorId);
        CUtil.logInfo("[创建表格] " + tableName);
        tableMapper.createTable(tableName);
    }

    /**
     * 查询所有数据表格名称
     *
     * @return {@link List }<{@link String }>
     */
    @Master
    @Override
    public List<String> listTableNames() {
        return tableMapper.listDataTableNames();
    }
}
